#include "heal.h"
#include "Ai.hpp"
#include "helper.h"

bool Heal::execute() {
    //recherche d'un objet de soin dzns l'inventaire dans juice, sinon achat en passant en resupply
//    value = ai.api->useEquipement(idParent->id, juice, idTarget);

    //test stuff
    map<int,Object>::iterator needle;
    if( (needle = idParent->items.find(EQUIPMENT_JUICE_NEEDLE)) != idParent->items.end() ){
        //test munitions
        if( needle->second.info > 0 ){
            //Heal
            Fruit* target;
            {
                map<int,Fruit>::iterator it;
                if( (it = ai.world.ourFruits.find( idTarget )) == ai.world.ourFruits.end() ) return false;
                target = &it->second;
            }
            if( idParent->useEquipment(needle->second.id,target->id) == OK ) return true;
            path.clear();
            pf.start = idParent->position;
            pf.end = target->position;
            if(pf.compute(&path,0,0)==false) return false;
            {
                list<map_chunk*>::iterator it = path.begin();
                for( int i = 0; i < idParent->speed; i++ ) if( it != path.end() )it++;
                if( it == path.end() ) it--;
                if( (*it) == target->position ) it--;
                if( idParent->move((*it)->X_coord, (*it)->Y_coord )!= OK ) return false;
                return true;
            }
        }
    }

    delete idParent->task;
    //idParent->task = new Resupply(T_RESUPPLY, idParent, EQUIPMENT_JUICE_NEEDLE);

    return true;
}


